package com.fingard.dsp.bank.directbank.icbc02;

import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.NodeSalaryItem;
import com.fingard.dsp.bank.directConfig.SalaryConfig;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqPayRecDetail;
import com.fingard.dsp.salaryset.SalaryDecrypt;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.util.RSAEncryptHelper;
import com.fingard.util.secret.salary.DESUtil;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ICBC02BatPayRec extends ICBC02Base implements IBatPayRec {

    // 批量代付，采用流处理方式
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988)
            throws Exception {
        String enableSalaryDecrypt = getBankConfig().getValueAsString("enableSalaryDecrypt");
        String commedTypeArrs = getBankConfig().getValueAsString("salaryDecryptCommand");
        if (("true".equals(enableSalaryDecrypt)) && (StringHelper.hasAnyChar(commedTypeArrs))) {
            String[] arrs = commedTypeArrs.split(",");
            List<String> list = Arrays.asList(arrs);
            if (list.contains(p_req1988.reqHeader.commandCode)) {
                if (StringHelper.isNullOrEmpty(p_req1988.decryptKey)) {
                    throw new Exception("获取密钥失败");
                }
                p_req1988.decryptKey = RSAEncryptHelper.base64PlusRSADecryptAndUncompress(p_req1988.decryptKey, "utf-8", SalaryDecrypt.rsaPrivateKey);
                p_req1988.isNeedDecrypt = true;
                this.isNeedHiddleField = Boolean.valueOf(true);
            }
            if (StringHelper.hasAnyChar(p_req1988.decryptKey)) {
                String key = p_req1988.reqHeader.bankCode + "-" + p_req1988.reqHeader.corporation
                        + "-" + p_req1988.reqHeader.group;
                SalaryConfig salaryConfig = SalaryDecrypt.getSalaryConfig(key);
                if (salaryConfig == null) {
                    throw new Exception("获取渠道：" + key + " 的解密配置失败");
                }
                Map<String, NodeSalaryItem> map = salaryConfig.nodeSalaryItemMap;
                p_req1988.salaryItemMap = map;
                if (map.keySet().contains("TotalAmount")) {
                    NodeSalaryItem item = map.get("TotalAmount");
                    String totalAmt = DESUtil.jdkDESDecryptMode(p_req1988.totalAmt, p_req1988.decryptKey);
                    if (StringHelper.hasAnyChar(totalAmt)) {
                        p_req1988.totalAmt = totalAmt.split(item.union)[(item.index - 1)];
                    }
                }
            }
        }
        if ("198803".equals(p_req1988.reqHeader.commandCode)) {
            return d_198803_BatPay(p_req1988);
        } else if ("198804".equals(p_req1988.reqHeader.commandCode)) {
            return d_198804_BatPay(p_req1988);
        } else if ("198811".equals(p_req1988.reqHeader.commandCode)) {
            return d_198811_BatPay(p_req1988);
        } else {
            // 返回ATS的对象
            RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
            try {
                bnkPackageID = p_req1988.reqBatchNO;
                bnkfSeqno = p_req1988.reqBatchNO;
                boolean hasOtherBk = p_req1988.hasOtherBank();// 是否存在跨行
                boolean isSameSpd = p_req1988.isSamePaySpd();// 普通加急标志是否都一样
                if (p_req1988.reqHeader.commandCode.equals("198802")) {// 批付，非财务室，即普通单笔调用的
                    bnkTransCode = "PAYENT";
                } else if ("198806".equalsIgnoreCase(p_req1988.reqHeader.commandCode) || (!StringHelper.isNullOrEmpty(p_req1988.sumAccountFlag) && !hasOtherBk)) {
                    // 汇总记账只支持同行
                    bnkTransCode = "PAYPERCOL";// 企业财务室汇总记账
                } else {
                    bnkTransCode = "PAYPER";// 企业财务室
                }

                XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
                // 写入ICBC02通用的头部节点
                Date tmpDateNow = new Date();
                writeCommonHeader(xmlWriter, isNeedHiddleField);
                String onlBatF = getBankConfig().getValueAsString("onlBatF");
                if (StringHelper.isNullOrEmpty(onlBatF)) {
                    onlBatF = "1";
                }
                xmlWriter.writeElementString("OnlBatF", onlBatF);
                if (bnkTransCode.equals("PAYPERCOL")) {// 企业财务室汇总记账
                    xmlWriter.writeElementString("BusType", p_req1988.sumAccountFlag);
                } else {
                    // /CMS/eb/in/SettleMode 入账方式 必输项 数字 1 2：并笔入账 0：逐笔记账
                    if (hasOtherBk || !isSameSpd) {// 有跨行，或者普通加急标志不一样，只能逐笔入账
                        xmlWriter.writeElementString("SettleMode", "0");
                    } else {
                        String settleMode1988 = getBankConfig().getValueAsString(
                                "settleMode1988");
                        if (StringHelper.isNullOrEmpty(settleMode1988)) {
                            settleMode1988 = "2";
                        }
                        xmlWriter.writeElementString("SettleMode", settleMode1988);
                    }
                }
                xmlWriter.writeElementString("TotalNum", p_req1988.totalNum);
                xmlWriter.writeElementString("TotalAmt",
                        StringHelper.amountMultiply100(p_req1988.totalAmt));
                xmlWriter.writeElementString("SignTime",
                        Format.DateTime17Format.format(tmpDateNow));
                xmlWriter.writeElementString("ReqReserved1", p_req1988.abstractATS);
                xmlWriter.writeElementString("ReqReserved2", p_req1988.reqBatchNO);
                // 需要进行对账码拼接以及节点解析，采用XElement进行处理
                int tmpRdCount = 0;
                while (p_req1988.readRecord()) {
                    tmpRdCount++;
                    // 单笔、批量共用节点赋值方法
                    //ATSES-24815 菲亚特：新增工行非企业财务室批量代付收款方所在城市名称默认值配置
                    String defautCity = "";
                    if (StringHelper.isNullOrEmpty(p_req1988.readedDetail.cnapsCode) && bnkTransCode.equals("PAYENT")) {
                        defautCity = getBankConfig().getValueAsString("BatPayCityDef");
                    }
                    XElement tmpXElement = convertPayRecDetailToBankXElement(p_req1988.readedDetail, tmpRdCount, p_req1988.reqBatchNO, defautCity);
                    xmlWriter.writeXElement(tmpXElement);
                }
                xmlWriter.writeEndDocument();// 写入报文完成
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), true);// 发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    // 解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1],
                            tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader
                                .getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetMsg = tmpBkReader
                                .getFirstText("/CMS/eb/pub/RetMsg");
                        if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                            retAts.respCode = "0";
                            retAts.transState = tmpRetCode;
                            retAts.respInfo = tmpRetMsg;
                            retAts.payInfoCode = tmpRetCode;
                            retAts.reqSeqID = tmpBkReader
                                    .getFirstText("/CMS/eb/pub/fSeqno");
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = "/CMS/eb/pub/RetCode节点为空";
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpStrRet[1];
                    retAts.reqSeqID = p_req1988.reqBatchNO;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.payInfoCode = tmpStrRet[0];
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }

            } catch (Exception e) {
                retAts.respCode = "-1";
                retAts.respInfo = e.toString();
                e.printStackTrace();
            }
            if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
                retAts.reqSeqID = p_req1988.reqBatchNO;
            }
            retAts.writeRetToATS();
            return retAts;
        }
    }


    public RetAtsBatPayRec d_198803_BatPay(ReqBatPayRec p_req1988)
            throws Exception {
        // 返回ATS的对象
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        try {
            boolean isSameSpd = p_req1988.isSamePaySpd();// 普通加急标志是否都一样
            if (isSameSpd) {
                bnkfSeqno = p_req1988.reqBatchNO;
                bnkPackageID = bnkfSeqno;
                bnkTransCode = "AGTEXCH";

                XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
                Date tmpDateNow = new Date();
                // 写入ICBC02通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeElementString("PayType", "1".equals(p_req1988.paySpd) ? "1" : "2");//支付方式 1:加急 2:普通
                xmlWriter.writeElementString("TotalNum", p_req1988.totalNum);
                xmlWriter.writeElementString("TotalAmt", StringHelper.amountMultiply100(p_req1988.totalAmt));
                xmlWriter.writeElementString("SignTime", Format.DateTime17Format.format(tmpDateNow));
                xmlWriter.writeElementString("ReqReserved1", "");//p_req1988.abstractATS);
                xmlWriter.writeElementString("ReqReserved2", "");//p_req1988.reqBatchNO);

                // 需要进行对账码拼接以及节点解析，采用XElement进行处理
                //int tmpRdCount = 0;
                while (p_req1988.readRecord()) {
                    //tmpRdCount++;
                    ReqPayRecDetail tmpDetail = p_req1988.readedDetail;
                    XElement tmpXElement = new XElement("rd", "");
                    tmpXElement.addChild(new XElement("iSeqno", tmpDetail.askNumber));//String.valueOf(tmpRdCount)
                    tmpXElement.addChild(new XElement("PayAgentAccNo", tmpDetail.payAct));
                    tmpXElement.addChild(new XElement("PayAgentName", tmpDetail.payName));
                    tmpXElement.addChild(new XElement("PayAccNo", tmpDetail.innerAct));
                    tmpXElement.addChild(new XElement("PayAccNameCN", tmpDetail.innerName));
                    tmpXElement.addChild(new XElement("PayAgentBranchInfo", tmpDetail.payBankLocation));
                    tmpXElement.addChild(new XElement("isICBCAcc", tmpDetail.sameBnk));
                    tmpXElement.addChild(new XElement("RecAreaName", tmpDetail.recAreaName));
                    tmpXElement.addChild(new XElement("RecNetName", "1".equals(tmpDetail.sameBnk) ? "" : tmpDetail.cnapsName));
                    tmpXElement.addChild(new XElement("RecAccNameCN", tmpDetail.recName));
                    tmpXElement.addChild(new XElement("RecAccNo", tmpDetail.recAct));
                    tmpXElement.addChild(new XElement("RecAgentBranchInfo", tmpDetail.cnapsName));
                    tmpXElement.addChild(new XElement("CurrType", convertCurToBank(tmpDetail.payCur)));
                    tmpXElement.addChild(new XElement("PayAmt", StringHelper.amountMultiply100(tmpDetail.payAmount)));
                    tmpXElement.addChild(new XElement("PayUse", tmpDetail.purpose));
                    tmpXElement.addChild(new XElement("ReqReserved3", tmpDetail.askNumber));
                    tmpXElement.addChild(new XElement("ReqReserved4", ""));
                    nodeAnalysis(tmpDetail.srcXElement, tmpXElement, getCharset());
                    xmlWriter.writeXElement(tmpXElement);
                }
                xmlWriter.writeEndDocument();// 写入报文完成
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), true);// 发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    // 解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                        if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                            retAts.respCode = "0";
                            retAts.transState = tmpRetCode;
                            retAts.respInfo = tmpRetMsg;
                            retAts.payInfoCode = tmpRetCode;
                            retAts.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = "/CMS/eb/pub/RetCode节点为空";
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpStrRet[1];
                    retAts.reqSeqID = p_req1988.reqBatchNO;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.payInfoCode = tmpStrRet[0];
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "该批次内的加急标识需保持一致！";
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_req1988.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec d_198804_BatPay(ReqBatPayRec p_req1988) throws Exception {
        // 返回ATS的对象
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        JSONObject result = d_198804_QryBatPay(p_req1988);
        WriteBankLogLn("代发登记簿查询接口返回：" + result.toString());
        String RegSerialNO = "";
        if (result.get("code").equals("1")) {
            RegSerialNO = result.get("msg").toString();
        } else {
            retAts.respCode = "-1";
            retAts.respInfo = result.get("msg").toString();
            return retAts;
        }
        try {
            bnkfSeqno = p_req1988.reqBatchNO;
            bnkPackageID = bnkfSeqno;
            bnkTransCode = "PAYPER";

            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            Date tmpDateNow = new Date();
            // 写入ICBC02通用的头部节点
            writeCommonHeader(xmlWriter);
            xmlWriter.writeElementString("OnlBatF", "1");
            xmlWriter.writeElementString("SettleMode", "0");// ÈëÕÊ·½Ê½2£º²¢±Ê¼ÇÕË0£ºÖð±Ê¼ÇÕË
            xmlWriter.writeElementString("TotalNum", p_req1988.totalNum);
            xmlWriter.writeElementString("TotalAmt", StringHelper.amountMultiply100(p_req1988.totalAmt));
            xmlWriter.writeElementString("SignTime", Format.DateTime17Format.format(tmpDateNow));
            xmlWriter.writeElementString("THBaseAccFlag", "1");// p_req1988.abstractATS);
            xmlWriter.writeElementString("RegSerialNO", RegSerialNO);// p_req1988.reqBatchNO);
            xmlWriter.writeElementString("PackageName", "");
            xmlWriter.writeElementString("TotalSummary", "");
            xmlWriter.writeElementString("BatchSumFlag", "");

            //int tmpRdCount = 0;
            while (p_req1988.readRecord()) {
                //tmpRdCount++;
                ReqPayRecDetail tmpDetail = p_req1988.readedDetail;
                XElement tmpXElement = new XElement("rd", "");
                tmpXElement.addChild(new XElement("iSeqno", tmpDetail.askNumber));//String.valueOf(tmpRdCount)
                tmpXElement.addChild(new XElement("ReimburseNo", ""));
                tmpXElement.addChild(new XElement("ReimburseNum", ""));
                tmpXElement.addChild(new XElement("StartDate", ""));
                tmpXElement.addChild(new XElement("StartTime", ""));
                //PayType 1£º¼Ó¼± 2£ºÆÕÍ¨
                if (!StringHelper.isNullOrEmpty(tmpDetail.paySpd) && tmpDetail.paySpd.equals("1")) {
                    tmpXElement.addChild(new XElement("PayType", "1"));
                } else {
                    tmpXElement.addChild(new XElement("PayType", "2"));
                }
                tmpXElement.addChild(new XElement("PayAccNo", getBankConfig().getMapValueByKey("BankAccountMapping", p_req1988.ownAct)));
                tmpXElement.addChild(new XElement("PayAccNameCN", getBankConfig().getMapValueByKey("BankNameMapping", p_req1988.ownAct)));
                tmpXElement.addChild(new XElement("PayAccNameEN", ""));
                tmpXElement.addChild(new XElement("RecAccNo", tmpDetail.recAct));
                tmpXElement.addChild(new XElement("RecAccNameCN", tmpDetail.recName));
                tmpXElement.addChild(new XElement("RecAccNameEN", ""));
                ////1：系统内 2：系统外
                if (tmpDetail.sameBnk.equals("1")) {
                    tmpXElement.addChild(new XElement("SysIOFlg", "1"));
                } else {
                    tmpXElement.addChild(new XElement("SysIOFlg", "2"));
                }
                tmpXElement.addChild(new XElement("IsSameCity", ""));
                tmpXElement.addChild(new XElement("RecICBCCode", ""));
                if (tmpDetail.sameBnk.equals("1")) {
                    tmpXElement.addChild(new XElement("RecCityName", ""));
                    tmpXElement.addChild(new XElement("RecBankNo", ""));
                    tmpXElement.addChild(new XElement("RecBankName", ""));
                } else {
                    tmpXElement.addChild(new XElement("RecCityName", tmpDetail.getOppProvinceAndCity2()[1]));
                    tmpXElement.addChild(new XElement("RecBankNo", tmpDetail.cnapsCode));
                    tmpXElement.addChild(new XElement("RecBankName", tmpDetail.cnapsName));
                }

                tmpXElement.addChild(new XElement("CurrType", convertCurToBank(tmpDetail.payCur)));
                tmpXElement.addChild(new XElement("PayAmt", StringHelper.amountMultiply100(tmpDetail.payAmount)));
                tmpXElement.addChild(new XElement("UseCode", ""));
                tmpXElement.addChild(new XElement("UseCN", "工资"));
                tmpXElement.addChild(new XElement("EnSummary", ""));
                tmpXElement.addChild(new XElement("PostScript", ""));
                tmpXElement.addChild(new XElement("Summary", ""));
                tmpXElement.addChild(new XElement("Ref", ""));
                tmpXElement.addChild(new XElement("Oref", ""));
                tmpXElement.addChild(new XElement("ERPSqn", ""));
                tmpXElement.addChild(new XElement("BusCode", ""));
                tmpXElement.addChild(new XElement("ERPcheckno", ""));
                tmpXElement.addChild(new XElement("CrvouhType", ""));
                tmpXElement.addChild(new XElement("CrvouhName", ""));
                tmpXElement.addChild(new XElement("CrvouhNo", ""));
                tmpXElement.addChild(new XElement("MCardNo", ""));
                tmpXElement.addChild(new XElement("MCardName", ""));
                nodeAnalysis(tmpDetail.srcXElement, tmpXElement, getCharset());
                xmlWriter.writeXElement(tmpXElement);
            }
            xmlWriter.writeEndDocument();// 写入报文完成
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), true);// 发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        retAts.respCode = "0";
                        retAts.transState = tmpRetCode;
                        retAts.respInfo = tmpRetMsg;
                        retAts.payInfoCode = tmpRetCode;
                        retAts.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = "/CMS/eb/pub/RetCode节点为空";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_req1988.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_req1988.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    /**
     * 批量境外外币支付提交
     *
     * @param p_req1988
     * @return
     * @throws Exception
     */
    public RetAtsBatPayRec d_198811_BatPay(ReqBatPayRec p_req1988) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        retAts.reqSeqID = p_req1988.reqBatchNO;

        try {
            bnkfSeqno = p_req1988.reqBatchNO;
            bnkPackageID = bnkfSeqno;
            bnkTransCode = "KJWHKK";

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter);

            xmlWriter.writeElementString("SignTime", Format.DateTime17Format.format(p_req1988.getPayDateTime()));
            xmlWriter.writeElementString("TotalNum", p_req1988.totalNum);
            xmlWriter.writeElementString("TotalAmt", StringHelper.amountMultiply100(p_req1988.totalAmt));
            xmlWriter.writeElementString("FileList", "", true);
            xmlWriter.writeElementString("IndexList", "", true);

            while (p_req1988.readRecord()) {
                ReqPayRecDetail tmpDetail = p_req1988.readedDetail;
                XElement tmpXElement = new XElement("rd", "");

                tmpXElement.addChild(new XElement("PayAcctName", tmpDetail.payName));
                tmpXElement.addChild(new XElement("PayAcctNo", tmpDetail.payAct));
                tmpXElement.addChild(new XElement("PayENAcctName", ""));
                tmpXElement.addChild(new XElement("PayAddr", tmpDetail.payCountryCode));
                tmpXElement.addChild(new XElement("Phone", tmpDetail.cellPhone));
                //1：系统内 2：系统外
                if (tmpDetail.sameBnk.equals("1")) {
                    tmpXElement.addChild(new XElement("SysIOFlag", "1"));
                } else {
                    tmpXElement.addChild(new XElement("SysIOFlag", "2"));
                }
                tmpXElement.addChild(new XElement("RecAcctName", tmpDetail.recName));
                tmpXElement.addChild(new XElement("RecAcctNo", tmpDetail.recAct));
                tmpXElement.addChild(new XElement("RecAddr", tmpDetail.recNameAddress));
                tmpXElement.addChild(new XElement("RecCountry", tmpDetail.recCountry));
                tmpXElement.addChild(new XElement("RecBankName", tmpDetail.cnapsName));
                tmpXElement.addChild(new XElement("RecBankCode", tmpDetail.cnapsCode));
                tmpXElement.addChild(new XElement("RecBankAddr", tmpDetail.recBankAddress));
                tmpXElement.addChild(new XElement("RecBankCountry", ""));
                tmpXElement.addChild(new XElement("RecBankCity", ""));
                tmpXElement.addChild(new XElement("RecAgtBankName", tmpDetail.recAgtBankName));
                tmpXElement.addChild(new XElement("RecAgeBankAddr", tmpDetail.recAgeBankAddr));
                tmpXElement.addChild(new XElement("CurrType", convertCurToBank(tmpDetail.recCur)));
                tmpXElement.addChild(new XElement("PayAmt", StringHelper.amountMultiply100(tmpDetail.payAmount)));
                tmpXElement.addChild(new XElement("TranNos", tmpDetail.transCode1));
                tmpXElement.addChild(new XElement("TPostscripts", tmpDetail.transInfo));
                tmpXElement.addChild(new XElement("TCurrTypes", convertCurToBank(tmpDetail.recCur)));
                tmpXElement.addChild(new XElement("TAmts", StringHelper.amountMultiply100(tmpDetail.payAmount)));
                tmpXElement.addChild(new XElement("Postscript", tmpDetail.remittanceInfor));
                tmpXElement.addChild(new XElement("KXProp", "1"));
                tmpXElement.addChild(new XElement("PayProp", tmpDetail.payProp));
                tmpXElement.addChild(new XElement("HXFlag", "0"));
                tmpXElement.addChild(new XElement("BSFlag", "0"));
                tmpXElement.addChild(new XElement("ZYDate", ""));
                tmpXElement.addChild(new XElement("ContractNo", tmpDetail.contractNo));
                tmpXElement.addChild(new XElement("BillNo", tmpDetail.invoiceNo));
                tmpXElement.addChild(new XElement("BUnitCode", ""));
                tmpXElement.addChild(new XElement("BPageNo", ""));
                tmpXElement.addChild(new XElement("BCurrType", ""));
                tmpXElement.addChild(new XElement("BTotalAmt", ""));
                tmpXElement.addChild(new XElement("HZAmt", ""));
                tmpXElement.addChild(new XElement("FeePayer", tmpDetail.feePayer));
                tmpXElement.addChild(new XElement("FeeAcctNo", ""));
                tmpXElement.addChild(new XElement("Remark", tmpDetail.memo));
                tmpXElement.addChild(new XElement("RecSortCode", ""));
                tmpXElement.addChild(new XElement("ExeDate", ""));
                tmpXElement.addChild(new XElement("RateMode", ""));
                tmpXElement.addChild(new XElement("Rate", ""));
                tmpXElement.addChild(new XElement("FxNum", ""));
                tmpXElement.addChild(new XElement("AbrhCode", ""));
                tmpXElement.addChild(new XElement("UniCode", ""));
                tmpXElement.addChild(new XElement("BusType", ""));
                tmpXElement.addChild(new XElement("PaySeqno", ""));
                tmpXElement.addChild(new XElement("RecSeqno", ""));
                tmpXElement.addChild(new XElement("FeeSeqNo", ""));
                tmpXElement.addChild(new XElement("SPNo", ""));
                tmpXElement.addChild(new XElement("FHProp", ""));
                tmpXElement.addChild(new XElement("PayCur", ""));
                tmpXElement.addChild(new XElement("FeeCur", ""));
                tmpXElement.addChild(new XElement("Iban", ""));
                tmpXElement.addChild(new XElement("IfSFHFlg", ""));
                tmpXElement.addChild(new XElement("MemberAccNoName", ""));
                tmpXElement.addChild(new XElement("IfLocalFlg", ""));
                tmpXElement.addChild(new XElement("LocalItemSet", ""));
                tmpXElement.addChild(new XElement("RecPhone", tmpDetail.cellPhone));
                tmpXElement.addChild(new XElement("PayUse", tmpDetail.purpose));
                tmpXElement.addChild(new XElement("Summary", tmpDetail.useType));
                tmpXElement.addChild(new XElement("RateContrno", ""));

                nodeAnalysis(tmpDetail.srcXElement, tmpXElement, getCharset());
                xmlWriter.writeXElement(tmpXElement);
            }
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), true);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                    retAts.transState = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    retAts.payInfoCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req9188.reqHeader);
        try {
            bnkPackageID = p_req9188.reqBatchNO;
            bnkfSeqno = p_req9188.reqBatchNO;
            boolean hasOtherBk = p_req9188.hasOtherBank();
            boolean isSameSpd = p_req9188.isSamePaySpd();
            if (p_req9188.reqHeader.commandCode.equals("918802")) {
                bnkTransCode = "ENTDIS";// 批扣企业指令提交
            } else if ("918803".equalsIgnoreCase(p_req9188.reqHeader.commandCode)
                    || "918804".equalsIgnoreCase(p_req9188.reqHeader.commandCode)
                    || (!StringHelper.isNullOrEmpty(p_req9188.sumAccountFlag) && !hasOtherBk)) {
                // 汇总记账只支持同行
                bnkTransCode = "PERDISCOL";// 批扣个人指令提交汇总记帐
            } else {
                bnkTransCode = "PERDIS";// 批扣个人指令提交
            }
            XmlTextWriter xmlWriter = new XmlTextWriter();
            // ------------ /CMS/eb/pub/ 开始------------
            writeCommonHeader(xmlWriter);
            // ------------ /CMS/eb/pub/ 结束------------
            // ------------ /CMS/eb/in/ 开始------------
            xmlWriter.writeElementString("OnlBatF", "1");

            if (bnkTransCode.equals("PERDISCOL")) {
                // 批扣个人指令提交汇总记帐
                xmlWriter.writeElementString("BusType", p_req9188.sumAccountFlag);
            } else if (bnkTransCode.equals("ENTDIS")) {
                xmlWriter.writeElementString("SettleMode", "0");
            } else {
                if (hasOtherBk || !isSameSpd) {
                    // 有跨行，或者普通加急标志不一样，只能逐笔入账
                    xmlWriter.writeElementString("SettleMode", "0");
                } else {
                    String settleMode1988 = getBankConfig().getValueAsString("settleMode9188");
                    if (StringHelper.isNullOrEmpty(settleMode1988)) {
                        settleMode1988 = "2";
                    }
                    xmlWriter.writeElementString("SettleMode", settleMode1988);
                }
            }
            xmlWriter.writeElementString("RecAccNo", p_req9188.ownAct);
            xmlWriter.writeElementString("RecAccNameCN", p_req9188.ownActName);
            xmlWriter.writeElementString("RecAccNameEN", "");
            if ("ENTDIS".equals(bnkTransCode)) {
                xmlWriter.writeElementString("PayType", "1");
            }
            xmlWriter.writeElementString("TotalNum", p_req9188.totalNum);
            xmlWriter.writeElementString("TotalAmt", StringHelper.amountMultiply100(p_req9188.totalAmt));
            xmlWriter.writeElementString("SignTime", Format.DateTime17Format.format(new Date()));
            xmlWriter.writeElementString("ReqReserved1", "");// p_req9188.abstractATS);
            xmlWriter.writeElementString("ReqReserved2", "");// p_req9188.reqBatchNO);
            // ------------ /CMS/eb/in/ 结束------------
            // ------------ /CMS/eb/in/rd/ 开始------------
            int tmpRdCount = 0;
            while (p_req9188.readRecord()) {
                tmpRdCount++;
                String clientNum = getBankConfig().getValueAsString("clientNum");
                if ("1".equals(clientNum)) {
                    XElement tmpXElement = convertRecDetailToBankXElement(p_req9188.readedDetail, tmpRdCount);
                    /*if ("error".equals(tmpXElement.name)) {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo("获取取缴费编号Portno失败");
                        retAts.writeEnd();
                        return retAts;
                    }*/
                    xmlWriter.writeXElement(tmpXElement);
                } else {
                    XElement tmpXElement = convertRecDetailToBankXElement(p_req9188.readedDetail, tmpRdCount);
                    xmlWriter.writeXElement(tmpXElement);
                }
            }
            // ------------ /CMS/eb/in/rd/ 结束------------
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), true);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        retAts.respCode = "0";
                        retAts.transState = tmpRetCode;
                        retAts.respInfo = tmpRetCode + "-" + tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                        retAts.payInfoCode = tmpRetCode;

                        String reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                        if (StringHelper.hasAnyChar(reqSeqID)) {
                            retAts.reqSeqID = reqSeqID;
                        } else {
                            retAts.reqSeqID = p_req9188.reqBatchNO;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = "/CMS/eb/pub/RetCode节点为空";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_req9188.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            e.printStackTrace();

            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            WriteBankLogLn(e);
        }
        retAts.writeRetToATS();
        return retAts;
    }

    // 1998 状态查询
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        if ("198803".equals(p_req1998.reqHeader.commandCode)) {
            return d_1998_QryBatPay_198803(p_req1998);
        } else if ("198811".equals(p_req1998.reqHeader.commandCode)) {
            return d_1998_QryBatPay_198811(p_req1998);
        } else {
            // 返回ATS的对象
            RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);

            bnkPackageID = p_req1998.reqHeader.transSeqID;
            if (p_req1998.reqHeader.commandCode.equals("198802")) {
                bnkTransCode = "QPAYENT";
            } else if (p_req1998.reqHeader.commandCode.equals("198802s")) {
                //华润银行加密专用
                boolean enableSalaryDecrypt = getBankConfig().getValueAsBool("enableSalaryDecrypt");
                if (enableSalaryDecrypt) {
                    isNeedHiddleField = true;
                }
                bnkTransCode = "QPAYPER";
            } else {
                bnkTransCode = "QPAYPER";
            }
            bnkfSeqno = bnkPackageID;
            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            writeCommonHeader(xmlWriter, isNeedHiddleField);// 写入ICBC02通用的头部节点

            xmlWriter.writeElementString("QryfSeqno", p_req1998.reqBatchNO);
            xmlWriter.writeElementClosed("QrySerialNo");
            xmlWriter.writeElementClosed("ReqReserved1");
            xmlWriter.writeElementClosed("ReqReserved2");

            xmlWriter.writeEndDocument();

            try {
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();

                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader
                                .getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetMsg = tmpBkReader
                                .getFirstText("/CMS/eb/pub/RetMsg");
                        if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                            if ("0".equals(tmpRetCode)
                                    || "B0116".equals(tmpRetCode)
                                    || "00000".equals(tmpRetCode)
                                    || "B0041".equals(tmpRetCode)) {
                                retAts.reqSeqID = tmpBkReader
                                        .getFirstText("/CMS/eb/out/QryfSeqno");
                                retAts.respCode = "0";
                                if ("B0116".equals(tmpRetCode)) {
                                    if (canBeNotExist(p_req1998, retAts, 70)) {
                                        retAts.respInfo = tmpRetMsg;
                                        retAts.transState = tmpRetCode;
                                        retAts.payInfoCode = tmpRetCode;
                                        if (StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
                                            retAts.reqSeqID = p_req1998.reqBatchNO;
                                        }
                                    } else {
                                        retAts.respCode = "-1";
                                        retAts.transState = "";
                                    }
                                } else if ("B0041".equals(tmpRetCode)) {
                                    if (!"所查指令已被整包拒绝：集团无此账号".equals(tmpRetMsg)) {
                                        retAts.respCode = "-1";
                                    }
                                    retAts.respInfo = tmpRetMsg;
                                    retAts.transState = tmpRetCode;
                                    retAts.payInfoCode = tmpRetCode;
                                }
                                while (tmpBkReader.read("/CMS/eb/out/rd")) {
                                    RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                                    tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("RecAccNameCN");
                                    tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("RecAccNo");
//                                    tmpRetDetail.payAmount = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("PayAmt"));
                                    tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleText("UseCN");
                                    tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("ERPSqn");
                                    tmpRetDetail.curType = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CurrType"));
                                    tmpRetDetail.result = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("Result");

                                    tmpRetDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("instrRetCode");
                                    if (StringHelper.isNullOrEmpty(tmpRetDetail.payInfoCode)) {
                                        tmpRetDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("iRetCode");
                                    }
                                    tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("instrRetMsg");
                                    if (StringHelper.isNullOrEmpty(tmpRetDetail.memo)) {
                                        tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("iRetMsg");
                                    }
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            } else {
                                retAts.respCode = "-1";
                                retAts.respInfo = tmpRetMsg;
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = "节点/CMS/eb/pub/RetCode为空";
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                retAts.respCode = "-1";
                retAts.respInfo = e.getMessage();
                e.printStackTrace();
            }
            if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
                retAts.reqSeqID = p_req1998.reqBatchNO;
            }
            retAts.writeAfterDetail();
            return retAts;
        }
    }

    public RetAtsQryBatPayRec d_1998_QryBatPay_198803(ReqQryBatPayRec p_req1998) throws Exception {
        // 返回ATS的对象
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);

        bnkPackageID = p_req1998.reqHeader.transSeqID;
        bnkTransCode = "QAGTEXCH";
        bnkfSeqno = bnkPackageID;
        XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
        writeCommonHeader(xmlWriter);// 写入ICBC02通用的头部节点

        xmlWriter.writeElementString("QryfSeqno", p_req1998.reqBatchNO);
        xmlWriter.writeElementClosed("QrySerialNo");
        xmlWriter.writeElementClosed("ReqReserved1");
        xmlWriter.writeElementClosed("ReqReserved2");
        // xmlWriter.writeStartElement("rd");
        // xmlWriter.writeElementString("iSeqno", "");
        // xmlWriter.writeElementString("QryiSeqno", "");
        // xmlWriter.writeElementClosed("QryOrderNo");
        // xmlWriter.writeElementClosed("ReqReserved3");
        // xmlWriter.writeElementClosed("ReqReserved4");
        xmlWriter.writeEndDocument();

        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        if ("0".equals(tmpRetCode) || "B0116".equals(tmpRetCode) || "00000".equals(tmpRetCode)) {
                            retAts.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/out/QryfSeqno");
                            retAts.respCode = "0";
                            if ("B0116".equals(tmpRetCode)) {
                                if (canBeNotExist(p_req1998, retAts, 70)) {
                                    retAts.respInfo = tmpRetMsg;
                                    retAts.transState = tmpRetCode;
                                    retAts.payInfoCode = tmpRetCode;
                                    if (StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
                                        retAts.reqSeqID = p_req1998.reqBatchNO;
                                    }
                                } else {
                                    retAts.respCode = "-1";
                                    retAts.transState = "";
                                }
                            }
                            retAts.writeBeforeDetail();
                            while (tmpBkReader.read("/CMS/eb/out/rd")) {
                                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                                tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("RecAccNameCN");
                                tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("RecAccNo");
                                tmpRetDetail.payAmount = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("PayAmt"));
                                tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleText("PayUse");
                                tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("QryiSeqno");
                                tmpRetDetail.result = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("Result");
                                tmpRetDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("iRetCode");
                                tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("iRetMsg");
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpRetMsg;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = "节点/CMS/eb/pub/RetCode为空";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_req1998.reqBatchNO;
        }
        if (retAts.respCode.equals("-1")) {
            retAts.writeBeforeDetail();
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    /**
     * 批量境外外币支付查询
     *
     * @param p_req1998
     * @return
     * @throws Exception
     */
    public RetAtsQryBatPayRec d_1998_QryBatPay_198811(ReqQryBatPayRec p_req1998) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
        retAts.reqSeqID = p_req1998.reqBatchNO;

        try {
            bnkPackageID = p_req1998.reqHeader.transSeqID;
            bnkTransCode = "QRYKJWHKK";
            bnkfSeqno = bnkPackageID;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter);

            xmlWriter.writeElementClosed("QrySerialNo");
            xmlWriter.writeElementString("QryfSeqno", p_req1998.reqBatchNO);
            xmlWriter.writeElementClosed("IsICBC");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        if ("0".equals(tmpRetCode) || "B0116".equals(tmpRetCode) || "00000".equals(tmpRetCode)) {
                            retAts.respCode = "0";
                            if ("B0116".equals(tmpRetCode)) {
                                if (canBeNotExist(p_req1998, retAts, 70)) {
                                    retAts.respInfo = tmpRetMsg;
                                    retAts.transState = tmpRetCode;
                                    retAts.payInfoCode = tmpRetCode;
                                    retAts.writeAfterDetail();
                                    return retAts;
                                }
                            }
                            while (tmpBkReader.read("/CMS/eb/out/rd")) {
                                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                                tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("RecAccNameCN");
                                tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("RecAccNo");
                                tmpRetDetail.payAmount = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("PayAmt"));
                                tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleText("PayUse");
                                tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("QryiSeqno");
                                tmpRetDetail.result = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("Result");
                                tmpRetDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("iRetCode");
                                tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("iRetMsg");
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpRetMsg;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = "节点'/CMS/eb/pub/RetCode'为空";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeAfterDetail();
        return retAts;
    }

    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req9198.reqHeader);
        retAts.reqSeqID = p_req9198.reqBatchNO;
        try {
            bnkPackageID = p_req9198.reqHeader.transSeqID;
            if (p_req9198.reqHeader.commandCode.equals("918802")) {
                bnkTransCode = "QENTDIS";// 批扣企业指令查询
            } else {
                bnkTransCode = "QPERDIS";// 批量扣个人指令查询
            }
            bnkfSeqno = bnkPackageID;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter);

            xmlWriter.writeElementString("QryfSeqno", p_req9198.reqBatchNO);
            xmlWriter.writeElementClosed("QrySerialNo");
            xmlWriter.writeElementClosed("ReqReserved1");
            xmlWriter.writeElementClosed("ReqReserved2");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        if (tmpRetCode.equals("0") || tmpRetCode.equals("B0116") || tmpRetCode.equals("00000") || "B0098".equals(tmpRetCode) || "B0097".equals(tmpRetCode)) {
                            retAts.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/out/QryfSeqno");
                            retAts.respCode = "0";
                            if ("B0116".equals(tmpRetCode) || "B0098".equals(tmpRetCode) || "B0097".equals(tmpRetCode)) {
                                if (canBeNotExist(p_req9198, retAts, 70)) {
                                    retAts.respInfo = tmpRetMsg;
                                    retAts.transState = tmpRetCode;
                                    retAts.payInfoCode = tmpRetCode;
                                    if (StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
                                        retAts.reqSeqID = p_req9198.reqBatchNO;
                                    }
                                } else {
                                    retAts.respCode = "-1";
                                    retAts.transState = "";
                                }
                            }
                            while (tmpBkReader.read("/CMS/eb/out/rd")) {
                                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                tmpRetDetail.payName = tmpBkReader.readedXElement.getSingleText("PayAccNameCN");
                                tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("PayAccNo");
                                tmpRetDetail.payAmount = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("PayAmt"));
                                tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleText("UseCN");
                                tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("ERPSqn");
                                tmpRetDetail.curType = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CurrType"));
                                tmpRetDetail.result = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("Result");
                                String iRetMsg = tmpBkReader.readedXElement.getSingleTextNotNull("iRetMsg");
                                iRetMsg = iRetMsg.length() == 0 ? " " : iRetMsg;
                                String iRetCode = tmpBkReader.readedXElement.getSingleText("iRetCode");
                                if ("9301".equals(iRetCode)) {
                                    tmpRetDetail.payInfoCode = iRetMsg;
                                } else {
                                    tmpRetDetail.payInfoCode = iRetCode;
                                }
                                String BankRem = tmpBkReader.readedXElement.getSingleTextNotNull("BankRem");
                                tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("iRetCode") + "-" + iRetMsg + "-" + BankRem;

                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpRetMsg;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = "节点/CMS/eb/pub/RetCode为空";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            e.printStackTrace();

            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();

            WriteBankLogLn(e);
        }

        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_req9198.reqBatchNO;
        }
        if (retAts.respCode.equals("-1")) {
            retAts.writeBeforeDetail();
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    public JSONObject d_198804_QryBatPay(ReqBatPayRec p_req1988) throws Exception {
        JSONObject result = new JSONObject();
        try {
            bnkTransCode = "THACRGLIST";
            bnkVersion = "0.0.1.0";
            String nextTag = " ";
            int qryCount = 0;
            StringBuffer RegSerialNo = new StringBuffer();
            while (!"".equals(nextTag)) {
                qryCount++;
                bnkfSeqno = p_req1988.reqBatchNO + String.valueOf(qryCount);
                bnkPackageID = bnkfSeqno;
                XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
                // 写入ICBC02通用的头部节点
                writeCommonHeader(xmlWriter);
                String Account = getBankConfig().getMapValueByKey("BankAccountMapping", p_req1988.ownAct);
                if (Account == null || "".equals(Account)) {
                    result.put("code", 0);
                    result.put("msg", "该账户没有配置BankAccountMapping映射");
                }
                xmlWriter.writeElementString("Account", Account);
                xmlWriter.writeElementString("NextTag", nextTag.trim());
                xmlWriter.writeEndDocument();// 写入报文完成

                WriteBankLogStep2("代发登记簿查询接口:" + xmlWriter.toXmlString());
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), false);// 发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    // 解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                        if (isActiveRetCode(tmpRetCode)) {
                            nextTag = tmpBkReader.getFirstText("/CMS/eb/out/NextTag");
                            WriteBankLogLn("当前nextTg:" + nextTag);
                            nextTag = antiTransferStr(nextTag);
                            WriteBankLogLn("反转义后nextTg:" + nextTag);
                            while (tmpBkReader.read("/CMS/eb/out/rd")) {
                                String Status = tmpBkReader.readedXElement.getSingleText("Status");
                                if (Status != null && "0".equals(Status)) {
                                    RegSerialNo.append(tmpBkReader.readedXElement.getSingleText("RegSerialNo") + "|");
                                }

                            }
                        } else {
                            result.put("code", 0);
                            result.put("msg", tmpRetMsg);
                            break;
                        }
                    } else {
                        result.put("code", 0);
                        result.put("msg", tmpSbMsg.toString());
                        break;
                    }
                } else {
                    result.put("code", 0);
                    result.put("msg", tmpStrRet[1]);
                    break;
                }
            }
            if (result.get("code") != null) {
                return result;
            }
            //去掉最后一个 |
            if (RegSerialNo.length() > 2) {
                RegSerialNo.setLength(RegSerialNo.length() - 1);
                result.put("code", 1);
                result.put("msg", RegSerialNo);
            } else {
                result.put("code", 1);
                result.put("msg", "");
            }
        } catch (Exception e) {

            e.printStackTrace();
            WriteBankLogLn(e);
        }
        return result;

    }
}
